<?php
namespace app\admin\service;

use app\model\RatingScaleFactorModel;
use app\model\RatingScaleQuestionAnswerModel;
use app\model\RatingScaleQuestionModel;
use app\model\RatingScaleQuestionRelatedFactorModel;
use think\Service;

class RatingScaleService extends Service
{

    /**
     * 预警列表符号列表
     * @return string[]
     */
    public function getSymbolList(): array
    {
        return [
            1 => '>=',
            2 => '<=',
            3 => '=',
        ];
    }

    /**
     * 预警标志
     * @return string[]
     */
    public function getFactorWarningMark(): array
    {
        return [
            1 => 'L1',
            2 => 'L2',
            3 => 'L3',
            4 => 'L4',
            5 => 'L5',
            6 => 'L6',
        ];
    }

    /**
     * 因子预警颜色
     * @return string[]
     */
    public function getFactorWarningColor(): array
    {
        return [
            1 => '#FFFCA4',
            2 => '#FFCD77',
            3 => '#FFAE00',
            4 => '#FD7F01',
            5 => '#FF4733',
            6 => '#A87DFF',
        ];
    }

    /**
     * 预警程度（等级）
     * @return array|string
     */
    public function getFactorLevel(): array|string
    {
        return [
            1 => '正常',
            2 => '轻度',
            3 => '中度',
            4 => '中重度',
            5 => '重度',
            6 => '异常（不分级）',
            7 => '总分异常'
        ];
    }

    /**
     * 预警程度（等级）名称
     * @param $level
     * @return string
     */
    public function getFactorLevelName($level): string
    {
        $data = $this->getFactorLevel();
        return $data[$level]??'';
    }


    /**
     * 获取量表题目
     * @param int $RatingScaleId 量表id
     * @return array
     */
    public function GetQuestion(int $RatingScaleId): array
    {
        $RatingScaleQuestion = new RatingScaleQuestionModel();
        $RatingScaleQuestionAnswer = new RatingScaleQuestionAnswerModel();
        $QuestionList = $RatingScaleQuestion->field('id,num,name,type')
            ->where('rating_scale_id','=',$RatingScaleId)->select()->toArray();
        $QuestionIds = array_column($QuestionList,'id');
        $QuestionAnswerList = $RatingScaleQuestionAnswer->field('id,question_id,name,score')
            ->where('question_id','in',$QuestionIds)->select()->toArray();
        $QuestionAnswerData = [];
        foreach ($QuestionAnswerList as $itemAnswer){
            $QuestionAnswerData[$itemAnswer['question_id']][] = $itemAnswer;
        }
        foreach ($QuestionList as &$item){
            $item['answer'] = $QuestionAnswerData[$item['id']]??[];
        }
        return $QuestionList;
    }

    /**
     * 获取题目详情
     * @param int $QuestionId 问题id
     * @return array|mixed
     */
    public function GetQuestionInfo(int $QuestionId): mixed
    {
        $RatingScaleQuestion = new RatingScaleQuestionModel();
        $RatingScaleQuestionAnswer = new RatingScaleQuestionAnswerModel();
        $RatingScaleQuestionRelatedFactor = new RatingScaleQuestionRelatedFactorModel();
        $info = $RatingScaleQuestion->field('id,num,name,type')->find($QuestionId);
        $info['answer'] = $RatingScaleQuestionAnswer->field('name,score')
            ->where('question_id',$QuestionId)->select()->toArray();
        $info['factor'] = $RatingScaleQuestionRelatedFactor->where('question_id',$QuestionId)
            ->with([
                'factor' => function($query){
                    $query->field('id,name');
                }
            ])->select()->toArray();
        return $info;
    }

    /**
     * 获取因子详情
     * @param int $FactorId 因子id
     * @return array|mixed
     */
    public function GetFactorInfo(int $FactorId): mixed
    {
        $RatingScaleFactor = new RatingScaleFactorModel();
        $RatingScaleQuestionRelatedFactor = new RatingScaleQuestionRelatedFactorModel();
        $info = $RatingScaleFactor->find($FactorId);
        $info['related'] = $RatingScaleQuestionRelatedFactor->where('factor_id',$FactorId)
            ->with([
                'question' => function($query){
                    $query->field('id,num,name');
                }
            ])->select()->toArray();
        return $info;
    }
}